See also: Rest Services In MDriven
When it comes to exposing ourselves to others – Turnkey has two MVC verbs, Get and Post, like this:
TurnkeyRest/Get?command=vmname&id=rootobjref TurnkeyRest/Post?command=vmname&id=rootobjref New(2023-04): --- if you have a varible vRestVerb it is assigned the verb used TurnkeyRest/Put?command=vmname&id=rootobjref TurnkeyRest/Patch?command=vmname&id=rootobjref TurnkeyRest/Delete?command=vmname&id=rootobjref <host>/Rest/vmname/Get/rootobjref <-- This format is also allowed for all verbs To send in extra parameters declare viewmodel variabels and they will recieve values: <host>/Rest/vmname/Get/rootobjref?MyVar=hello&MyOtherVar=world use Disable/ReadOnly expression on actions to decide what to run: vRestVerb<>'Put' -- readonly for all but Put
See Improved routes for other URLs.
The ViewModel name is supplied as the command parameter.
The id parameter is an object reference in one of several available formats. Read more here about how to create these.
Caution: Always send a complete ID, i.e. a guid or xx|yyyy. Using a shortcut without a class identifier can cause problems.
Note that the variables set in your ViewModel have to be of the String type.
The Commands Do This:
We check that the tagged value RestAllowed has been set on the ViewModel, then look up the root object.
When the ViewModel and its root have been found, we check the accessgroups to see if access is allowed.
Then additional parameters are set (these can be either URL Encoded or multi-part form-encoded. See HTTP POST for details).
- For Get, the parameter names are looked up against ViewModel variables, and variable values are set.
- For Post, the parameter names are looked up against ViewModel variables, and attributes and values are set.
Then any actions present at the root level of the ViewModel are executed - in the order presented in the ViewModel from top to bottom.
- For Get, use the actions to look up additional information.
- For Post, the actions are usually used to process sent JSON data into objects (see below)
Post saves any changed values to the database.
Both Get and Post return the ViewModel content as JSON in the HTTP response. To override this, use one of these strategies:
- Add a string attribute named exactly RawJSon on the root - if found, this is returned instead of the complete VM as JSON.
- Add a vReturnMessage:String variable as described further down this page
If there is an error, a string “error: <message>” is returned.
If you need to receive Post data unknown at design time, please read: Receive post data not known at design time.
You may use C# code to post to TurnkeyRest.
Processing JSON into Objects
If you want to send data as JSON in a POST, you need to apply the JSON string to your system's modeled objects.
Use Tajson or the simpler JsonToObjects. Read more here on XML or JSON support: Import xml and JSon with MDriven
The selfVM.JSonToObjects creates objects with the root of a class and matches attributes and associations from the JSON data – it can create object trees (unclosed graphs) by following names on associations.
selfVM.JSonToObjects( «<Type>» , JSonDataInStringFormat)
You can use an existing JSON as a template to create a model section you can import into - see: Using JSON or XML as a class template.
Video
To make your experience smooth, we set the main tags mentioned in the video to the right bar menu of this mini-player. Choose an interesting subtitle on the list and immediately get to the exact theme navigation item place in the video. Now you can pick any topic to be instructed on without watching the whole video.
Return Status Codes and Override Returned Data
New from 2020-12-09: You can now override return result data and code by declaring variables in your RestAllowed ViewModel:
vReturnMessage:String vReturnStatusCode:String
The vReturnMessage (reason code) must be found and will be returned as content instead of the default (to make JSON of the ViewModel). The return message will have the status code "vReturnStatusCode".
If the vReturnStatusCode is found, it must have one of the values defined here: https://docs.microsoft.com/en-us/dotnet/api/system.net.httpstatuscode?view=net-5.0 Otherwise, 200 "Ok" is returned. We will parse the string value with this logic:
realstatuscode = (System.Net.HttpStatusCode)System.Net.HttpStatusCode.Parse(typeof(System.Net.HttpStatusCode), (string)(returnstatuscode.AsObject), true); This means the action code can look like this: vReturnStatusCode:='NotFound' or vReturnStatusCode:='Created' etc
Read Status Code from RestPost, RestGet, etc
Use the vReturnStatusCode:string variable name to get Status from querying others with rest - RestPost, RestGet, etc.
Use the vReturnMessage:string variable name to get the reasoncode from others with Rest.
Receive String Content
We assume postback data as FormFields that we match to ViewModelColumns. If someone posts string content (no form data), we now put that data in a column named "STRINGCONTENT" if found.
Debugging
It may sometimes be time-consuming to get all the parameters correct when forming a request. A tip is to use postman-echo.com [1] to better understand what is sent.